package com.yeyks.consumer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.PageParam;
import com.yeyks.common.param.activity.ActivityTSStatParam;
import com.yeyks.common.result.activity.ActivityTSResult;
import com.yeyks.common.result.activity.ActivityUserInviteResult;
import com.yeyks.common.utils.ObjectConvertUtil;
import com.yeyks.consumer.dal.dao.UserInviteWhiteListMapper;
import com.yeyks.consumer.dal.domain.UserInviteWhiteList;
import com.yeyks.consumer.service.UserInviteRelationService;
import com.yeyks.consumer.service.UserInviteWhiteListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * <p>
 * 用户推广白名单表 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-12-06
 */
@Service
public class UserInviteWhiteListServiceImpl extends ServiceImpl<UserInviteWhiteListMapper, UserInviteWhiteList> implements UserInviteWhiteListService {


    @Autowired
    private UserInviteRelationService userInviteRelationService;
    @Autowired
    private JdbcTemplate jdbcTemplate;



    //根据userid查询
    @Override
    public UserInviteWhiteList getByUserId(Integer userId) {
        return getOne(
                new QueryWrapper<UserInviteWhiteList>()
                        .eq(UserInviteWhiteList.USER_ID, userId)
                        .eq(UserInviteWhiteList.STATUS, 1)
                        .last("limit 1")
        );
    }

    //判断用户是否在白名单
    @Override
    public boolean isWhite(Integer userId) {
        Integer value = jdbcTemplate.queryForObject("SELECT `value` FROM `config` where `key` = 'UserInviteWhiteListServiceImpl'", Integer.class);
        if(Objects.equals(value,1)){
            return true;
        }else {
            return Objects.nonNull(getByUserId(userId));
        }
    }

    /**
     * v3.8.2活动推手总统计
     *
     * @param param
     * @return
     */
    @Override
    public List<ActivityTSResult> statTotal(ActivityTSStatParam param) {
        if (1 == param.getDateType()) {
            if (null == param.getStartTimeDay() || null == param.getEndTimeDay()) {
                throw new ServiceException("天时间必传");
            }
            param.setStartTime(new Date(param.getStartTimeDay()));
            param.setEndTime(new Date(param.getEndTimeDay()));
        } else {
            if (null == param.getStartTimeMonth() || null == param.getEndTimeMonth()) {
                throw new ServiceException("月时间必传");
            }
            param.setStartTime(new Date(param.getStartTimeMonth()));
            param.setEndTime(new Date(param.getEndTimeMonth()));
        }
        return baseMapper.statTotal(param);
    }

    @Override
    public List<ActivityUserInviteResult> queryByPage(PageParam pageParam, Integer type) {

        List<ActivityUserInviteResult> activityUserInviteResults = Lists.newArrayList();
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<UserInviteWhiteList> userInviteWhiteLists = this.baseMapper.selectList(
                new QueryWrapper<UserInviteWhiteList>().eq(UserInviteWhiteList.TYPE, type)
                        .orderByDesc(UserInviteWhiteList.CREATE_TIME));
        if(!CollectionUtils.isEmpty(userInviteWhiteLists)){
            activityUserInviteResults = ObjectConvertUtil.convertList(userInviteWhiteLists, ActivityUserInviteResult.class);
        }

        return activityUserInviteResults;
    }

}
